/**
 * Copyright (c) 2011-2013, kidzhou 周磊 (zhouleib1412@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jfsnpm.plugin.jfinal.autoroutes;

import java.util.List;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;

public class AutoBindRoutes extends Routes {
	private boolean autoScan = true;
	private List<Class<? extends Controller>> excludeClasses = Lists
			.newArrayList();
	private boolean includeAllJarsInLib;
	private List<String> includeJars = Lists.newArrayList();
	protected final Logger logger = Logger.getLogger(getClass());
	private String suffix = "Controller";

	public AutoBindRoutes autoScan(boolean autoScan) {
		this.autoScan = autoScan;
		return this;
	}

	public AutoBindRoutes addExcludeClasses(
			Class<? extends Controller>... clazzes) {
		if (clazzes != null) {
			for (Class<? extends Controller> clazz : clazzes) {
				excludeClasses.add(clazz);
			}
		}
		return this;
	}

	public AutoBindRoutes addExcludeClasses(
			List<Class<? extends Controller>> clazzes) {
		excludeClasses.addAll(clazzes);
		return this;
	}

	public AutoBindRoutes addJars(String... jars) {
		if (jars != null) {
			for (String jar : jars) {
				includeJars.add(jar);
			}
		}
		return this;
	}

	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void config() {
		List<Class<? extends Controller>> controllerClasses = ClassSearcher
				.of(Controller.class).includeAllJarsInLib(includeAllJarsInLib)
				.injars(includeJars).search();
		ControllerBind controllerBind = null;
		for (Class controller : controllerClasses) {
			if (excludeClasses.contains(controller)) {
				continue;
			}
			controllerBind = (ControllerBind) controller
					.getAnnotation(ControllerBind.class);
			if (controllerBind == null) {
				if (!autoScan) {
					continue;
				}
				this.add(controllerKey(controller), controller);
				logger.debug("routes.add(" + controllerKey(controller) + ", "
						+ controller.getName() + ")");
			} else if (StrKit.isBlank(controllerBind.viewPath())) {
				this.add(controllerBind.controllerKey(), controller);
				logger.debug("routes.add(" + controllerBind.controllerKey()
						+ ", " + controller.getName() + ")");
			} else {
				this.add(controllerBind.controllerKey(), controller,
						controllerBind.viewPath());
				logger.debug("routes.add(" + controllerBind.controllerKey()
						+ ", " + controller + "," + controllerBind.viewPath()
						+ ")");
			}
		}
	}

	private String controllerKey(Class<Controller> clazz) {
		Preconditions
				.checkArgument(
						clazz.getSimpleName().endsWith(suffix),
						clazz.getName()
								+ " is not annotated with @ControllerBind and not end with "
								+ suffix);
		/*String controllerKey = "/"
				+ StrKit.firstCharToLowerCase(clazz.getSimpleName());*/
		String controllerKey = "/"
				+ clazz.getSimpleName();
		controllerKey = controllerKey.substring(0,
				controllerKey.indexOf(suffix));
		return controllerKey;
	}

	public AutoBindRoutes includeAllJarsInLib(boolean includeAllJarsInLib) {
		this.includeAllJarsInLib = includeAllJarsInLib;
		return this;
	}

	public AutoBindRoutes suffix(String suffix) {
		this.suffix = suffix;
		return this;
	}
}